package ysq.xyg.recovery.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.recovery.dao.RecoveryOrderDao;
import ysq.xyg.recovery.model.po.RecoveryOrder;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.recovery.model.vo.RecoveryOrderVo;
import ysq.xyg.recovery.service.RecoveryOrderService;
import ysq.xyg.web.model.po.WebMember;
import ysq.xyg.web.service.WebMemberService;
import ysq.xyg.wxapi.util.AmountUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 回收金模块-订单管理service实现
 *
 * @author congcong tao
 * @date 2019-7-11 20:30:57
 */
@Service("recoveryOrderService")
public class RecoveryOrderServiceImpl extends BaseServiceImpl<RecoveryOrder> implements RecoveryOrderService {

    @Autowired
    private WebMemberService webMemberService;
    @Resource
    private RecoveryOrderDao recoveryOrderDao;

    private RecoveryOrder recoveryOrder;

    @Autowired
    public void setBaseDao(RecoveryOrderDao recoveryOrderDao) {
        this.baseDao = recoveryOrderDao;
    }

    /**
     * 分页查询
     *
     * @param recoveryOrderVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-7-11 20:30:57
     */
    @Override
    public ReturnEntity page(RecoveryOrderVo recoveryOrderVo, int pageSize, int pageNum) {
        if (StringUtils.isNotEmpty(recoveryOrderVo.getMemberNickName())) {
            recoveryOrderVo.setMemberNickName(BaseUtil.encodeBase64String(recoveryOrderVo.getMemberNickName()));
        }
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<RecoveryOrder> list = recoveryOrderDao.selectEntityList(recoveryOrderVo);
        if (list != null && list.size() > 0) {
            for (RecoveryOrder recoveryOrder : list) {
                recoveryOrder.setCreateTimeStr(DateUtil.formatDateTime(recoveryOrder.getCreateTime()));
                recoveryOrder.setMemberNickName(BaseUtil.decodeBase64(recoveryOrder.getMemberNickName()));
            }
        }
        PageInfo<RecoveryOrder> page=new PageInfo<RecoveryOrder>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    @Override
    public ReturnEntity pageLeft(RecoveryOrderVo recoveryOrderVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<RecoveryOrder> list = acceptList(recoveryOrderVo);
        PageInfo<RecoveryOrder> page = new PageInfo<RecoveryOrder>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param recoveryOrderVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-7-11 20:30:57
     */
    @Override
    public ReturnEntity addOrUpdate(RecoveryOrderVo recoveryOrderVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(recoveryOrderVo.getId())){
            recoveryOrder=new RecoveryOrder(currentUser);
            isUpate=false;
        }else{
            recoveryOrder=selectByPrimaryKey(recoveryOrderVo.getId());
            if(ObjectUtil.isNull(recoveryOrder)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            recoveryOrder.setUpdateTime(new Date());
            recoveryOrder.setUpdateUser(currentUser.getId());
        }
        recoveryOrder.setMemberId(recoveryOrderVo.getMemberId());
        recoveryOrder.setNum(recoveryOrderVo.getNum());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(recoveryOrder);
        }else{
            num=insert(recoveryOrder);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public ReturnEntity saveOrder(String memberId, String orderNumber, long money, int num) {
        // 插入订单
        WebMember webMember = webMemberService.selectByPrimaryKey(memberId);
        RecoveryOrder recoveryOrder = new RecoveryOrder();
        recoveryOrder.setId(BaseUtil.uId());
        recoveryOrder.setMemberId(webMember.getId());
        recoveryOrder.setOrderNumber(orderNumber);
        // 分转为元
        String s = "0";
        try {
            s = AmountUtils.changeF2Y(money);
        } catch (Exception e) {
            e.printStackTrace();
        }
        recoveryOrder.setMoney(s);
        recoveryOrder.setNum(num);
        recoveryOrder.setStatus("1");
        recoveryOrder.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        recoveryOrder.setCreateTime(DateUtil.date());
        recoveryOrder.setUpdateTime(DateUtil.date());
        int info = this.insert(recoveryOrder);
        if (info <= 0) {
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-7-11 20:30:57
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        RecoveryOrder recoveryOrder=selectByPrimaryKey(id);
        if(recoveryOrder!=null){
            recoveryOrder.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            recoveryOrder.setUpdateTime(new Date());
            recoveryOrder.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(recoveryOrder);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param recoveryOrderVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-7-11 20:30:57
     */
    @Override
    public List<RecoveryOrder> acceptList(RecoveryOrderVo recoveryOrderVo){
        Example example = new Example(RecoveryOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(recoveryOrderVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",recoveryOrderVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return recoveryOrderDao.selectByExample(example);
    }

    @Override
    public int selectSumNum(String memberId) {
        return recoveryOrderDao.selectSumNum(memberId);
    }
}
